<template>
  <!-- 最外层标签的class名字使用组件名     -->
  <div class="login">
    <img class="bgimg" src="@/assets/img/login_bg.png" alt=""/>
    <div class="left">
      <div class="title">
        <img class="logo" src="@/assets/img/logo.png" alt />
        <p class="titleName2">用户登录</p>
      </div>
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="username" class="pwd">
          <label><img src="@/assets/img/user.png"/>账号:</label>
          <el-input v-model="form.username" placeholder="请输入账户昵称"></el-input>
        </el-form-item>
        <el-form-item prop="password" class="pwd">
          <label><img src="@/assets/img/pwd.png"/>密码:</label>
          <el-input
            :show-password="true"
            v-model="form.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="isCheck" class="rule">
          <el-checkbox v-model="form.isCheck">
            我已阅读并同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" @click="loginClick">登录</el-button>
          <br />
          <!-- <el-button class="btn" type="primary" @click="registerClick">注册</el-button> -->
        </el-form-item>
      </el-form>
    </div>
    <!-- <div class="right">
      <img src="@/assets/img/login_banner_ele.png" alt />
    </div> -->
    <!-- 组件中套用其它组件
    1：导入子组件   import 名字 from 路径
    2:注册   components:{register}
    3:当标签使用  
    
    
    -->
    <register ref="register"></register>
  </div>
</template>

<script>
import { toLogin } from "@/api/login.js";
import register from "./register.vue";
import { saveToken,getToken } from "@/utils/token.js";

export default {
  name: "login",
  components: {
    register
  },
  data() {
    return {
      // code: process.env.VUE_APP_URL + "/captcha?type=login",
      //表单绑定的值
      form: {
        // phone: "", //手机号
        username:'',//账户名称
        password: "", //密码
        // code: "", //验证码
        isCheck: "" //   是否同意协议
      },
      //表单验证规则
      rules: {
        username:[
          { required: true, message: "请输入账户名称", trigger: "change" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 6,
            max: 12,
            message: "请输入6到12位长度密码",
            trigger: "change"
          }
        ],
        isCheck: [
          { required: true, message: "请勾选选协议", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (value === true) {
                callback();
              } else {
                callback("请勾选选协议");
              }
            },
            trigger: "change"
          }
        ]
      }
    };
  },
  mounted() {},
  methods: {
    // 登陆点击
    loginClick() {
      // 登陆全局校验
      this.$refs.form.validate(result => {
        // 登陆接口调用
        if (result == true) {
          delete this.form.isCheck;
          toLogin(this.form).then(res => {
            if(res.code === 200){
              this.$message.success("登陆成功");
              saveToken(res.result.token);
              this.$router.push("/index")
            }else{
              this.$message.error(res.message);
            }
          });
        }
      });
    },
 
    // 注册点击
    registerClick() {
      // 打开register里的弹窗组件
      // 1：在register组件 上定义一个ref属性  ref=值
      //2：通过this.$refs.值.dialogFormVisible=true
      this.$refs.register.dialogFormVisible = true;
    },
  },
     created() {
      if(getToken()){
        this.$router.push("/");
      }
    },
};
</script>

<style lang="less" scoped>
.login {
  // 弹性盒子布局
  display: flex;
  //   主轴布局
  justify-content: center;
  //   侧轴居中
  align-items: flex-start;
  height: 100%;
  padding: 8% 0 0;
  background: #3664EC;
  position: relative;
  .bgimg{
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: 1;
  }
  .left {
    width: 420px;
    // height: 550px;
    background: #FFFFFF;
    border-radius: 20px;
    padding: 30px 40px 35px;
    z-index: 99;
    .title {
      margin-bottom: 20px;
      .logo{
        width: 151px;
        height: 38px;
        display: block;
        margin:0 auto 40px;
      }
      .titleName2 {
        font-size: 16px;
        font-weight: 600;
        color: #333333;
        line-height: 22px;
        text-align: center;
      }
    }
    .pwd{
      label{
        width: 75px;
        display: flex;
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        line-height: 20px;
        // justify-content: center;
        align-items: center;
        img{
          width: 16px;
          height: 16px;
          display: block;
          margin-right: 5px;
        }
      }
    }
    
    .key {
      width: 100%;
      height: 40px;
    }
    .btn {
      width: 100%;
      background: #4370F3;
      color: #fff;
    }
    // .btn:nth-child(1) {
    //   margin-bottom: 26px;
    // }
  }
}
</style>